package com.tedu.weibo.controller;

import com.tedu.weibo.mapper.UserMapper;
import com.tedu.weibo.pojo.dto.UserLoginDTO;
import com.tedu.weibo.pojo.dto.UserRegDTO;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.vo.UserVO;
import com.tedu.weibo.result.JsonResult;
import com.tedu.weibo.result.Status;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/v1/users")
@Slf4j
@Api(tags="用户模块")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("/reg")
    @ApiOperation(value = "用户注册")
    public JsonResult reg(@RequestBody UserRegDTO userRegDTO){
        log.trace("UserController.reg()开始被调用...");
        log.debug("UserRegDTO"+userRegDTO);
        log.info("开始处理用户注册");
        System.out.println("开始处理用户注册");
       if (userMapper.selectByUsername(userRegDTO.getUsername())!=null){
           return new JsonResult(Status.USERNAME_EXIST);
       }
        User user=new User();
        BeanUtils.copyProperties(userRegDTO,user);
        user.setCreated(new Date());
        log.debug("User"+user);
       userMapper.insert(user);
       log.trace("UserController.reg()调用完毕");
        return JsonResult.success();
    }
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public JsonResult login(@RequestBody @Validated UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session){
        System.out.println("开始处理用户登录");
//        String password=userMapper.selectByUsername(userLoginDTO.getUsername()).getPassword();
//        if (userMapper.selectByUsername(userLoginDTO.getUsername())==null){
//            return 3;
//        } else if (!userLoginDTO.getPassword().equals(password)) {
//            return 2;
//        }
//        System.out.println(userLoginDTO);
//        return 1;
        /*
            登录逻辑:
            1:首先根据用户输入的登录用户的用户名查询该用户信息
            2:如果没有查询到用户信息，则表示用户名输入错误，返回3
            3:如果查询到用户信息，则使用登录密码与查询到的用户密码比对
            4:密码比对一致，说明登录成功，返回1
            5:密码比对不一致，说明密码错误，返回2
         */
        System.out.println("开始用户登录,"+userLoginDTO);
        User user = userMapper.selectByUsername(userLoginDTO.getUsername());
        if(user!=null){//查询到该用户
            if(user.getPassword().equals(userLoginDTO.getPassword())){
                //密码正确，登录成功
                //将当前用户的信息存入session
                session.setAttribute("user",user);//相当于Map.put(key,value)
                return JsonResult.success();
            }else{
                return new JsonResult(Status.PASSWORD_ERROR);
            }
        }else{//没有该用户，用户名输入有误
            return new JsonResult(Status.USERNAME_ERROR);
        }
    }
    @GetMapping("/currentUser")
    @ApiOperation(value="查看用户信息")
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        User user= (User) session.getAttribute("user");
        if (user!=null){
            UserVO userVO=new UserVO();
            BeanUtils.copyProperties(user,userVO);
            return JsonResult.success(userVO);
        }
        return new JsonResult(Status.NOT_LOGIN);
    }
    @GetMapping("/logout")
    @ApiOperation(value="用户退出登录")
    public JsonResult logout(@ApiIgnore HttpSession session){
     session.removeAttribute("user");
     return JsonResult.success();
    }
}
